<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>

<body>
  <script>
    //动态属性
    let person = new Object();
    person.name = "Sloan";
    console.log(person.name);//Sloan

    let name1 = "Nicholas";
    let name2 = new String("Matt");
    name1.age = 23;
    name2.age = 26;
    console.log(name1.age);//undefined
    console.log(name2.age);//26
    console.log(typeof name1, typeof name2);//string,object
    //原始值复制
    let num1 = 5;
    let num2 = num1;
    console.log(num1, num2);//5 5 相互独立且互不干扰
    //引用值复制
    let obj1 = new Object();
    let obj2 = obj1;
    obj1.name = "Sloan";
    console.log(obj2.name);//Sloan;
    //传递参数
    /*
    参数num是一个局部变量,变量count作为参数传入,值20被复制到num中.
    参数num与变量count互不干扰,num是按值传递的
    */
    function addTen(num) {
      num += 10;
      return num;
    }
    let count = 20;
    let result = addTen(count);
    console.log(count);//20
    console.log(result);//30
    /*
    obj指向的是全局作用域上的p,当在函数内部给obj设置了name属性后,函数外部的
    对象也会反映这个变化.但是将obj重新定义为一个新的Object后,并把name属性改为Grep,函数
    外部的p并没有变化,说明p仍然是按值传递的,obj被重写时,它变成了一个指向本地对象的指针,
    这个本地对象在函数执行结束时就已经被销毁了.
    */
    function setName(obj) {
      obj.name = "Li Shi long";
      obj = new Object();
      obj.name = "Grep";
    }
    let p = new Object();
    setName(p);
    console.log(p.name);// Li Shi long

    let reg = /\d{11}/g;
    console.log(typeof reg);//object

    //执行上下文
    var color = "blue";

    function changeColor() {
      console.log(arguments);
      if (color === "blue") {
        color = "red";
      } else {
        color = "blue";
      }
    }
    changeColor();

    var color1 = "blue";
    function changeColor() {
      let anotherColor = "red";
      function swapColors() {
        let tempColor = anotherColor;
        anotherColor = color1;
        color1 = tempColor;
        // 这里可访问color1,anotherColor,tempColor.
      }
      //这里可以访问color1,anotherColor.
      swapColors()
    }
    // 这里只能访问color1.
    changeColor();

    //作用域链增强
    function buildUrl(){
      let qs = "?debug=true";
      with(location){
        let url = href + qs;
      }
      return url; //url is not define
    }
    //var函数作用域+声明提升.
    console.log(n); //undefined
    var n = "gg";
    //let块级作用域.
    if(true){
      let a;
    }
    // console.log(a);  a is not define
    const o = Object.freeze({});
    o.name = "Jake";
    console.log(o.name);//undefined
    //标识符查找
    var color2 = "blue";
    function getColor(){
      let color2 = "red";
      {
        // 块级作用域声明不会改变搜索流程,但会给词法添加额外的层次.
        let color2 = "green";
        return color2;
      }
    }
    console.log(getColor()); //green
  </script>
</body>

</html>